package com.thf.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thf.model.ResultData;
import com.thf.model.UserModel;
import com.thf.service.UserService;
import com.thf.util.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class LoginController{
    /**
     * doget和dopost的区别？
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//     0。乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //        1.得到前端传递过来的数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String captchaUser = req.getParameter("captcha");
        String phoneCode = req.getParameter("smsCode");
        String captchaSys = req.getSession().getAttribute("captcha").toString();
//        String smsCode = req.getSession().getAttribute("smsCode").toString();
        String smsCode ;
        Jedis jedis = new Jedis();
        if(jedis.exists("smsCode")){
            smsCode = jedis.get("smsCode");
        }else {
            String resJson = JSON.toJSONString(new ResultData(ResultTool.PHONE_CODE_ERROR, ResultTool.PHONE_CODE_ERROR_MSG));
//            System.out.println(resJson);
//            resp.getWriter().println(resJson);
            return resJson;
        }
        if(!captchaSys.equalsIgnoreCase(captchaUser)){
            String resJson = JSON.toJSONString(new ResultData(ResultTool.CAPTCHA_CODE_ERROR, ResultTool.CAPTCHA_CODE_ERROR_MSG));
//            System.out.println(resJson);
//            resp.getWriter().println(resJson);
            return resJson;
        }
        if(!smsCode.equals(phoneCode)){
            String resJson = JSON.toJSONString(new ResultData(ResultTool.PHONE_CODE_ERROR, ResultTool.PHONE_CODE_ERROR_MSG));//            System.out.println(resJson);
//            resp.getWriter().println(resJson);
            return resJson;
        }
//          2.封装对象
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassword(password);
        //返回值
        ResultData result = userService.login(userModel);
        //ResultData result = null;
        if (result.getCode() != 1 ) {
            req.getSession().setAttribute("username", username);
        }
//       反馈给前端--返回值
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
//        resp.getWriter().println(jsonObject);
        return jsonObject.toJSONString();
    }
}
